package com.access.validator.gateway;

import cn.hutool.core.util.StrUtil;
import com.access.annotation.AccessPermission;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: lei
 * @date: 2024-07-29 11:00
 * @desc: 校验网关请求注解
 */
public class GatewayValidator {

    public static final String GATEWAY = "Gateway";

    /**
     * 网关（内外部接口）验证器
     *
     * @param request
     * @param handler
     * @return
     */
    public static Boolean valid(HttpServletRequest request, HandlerMethod handler){
        if (StrUtil.isNotBlank(request.getHeader(GATEWAY))){
            AccessPermission classAnnotation = handler.getClass().getAnnotation(AccessPermission.class);
            AccessPermission methodAnnotation = handler.getMethodAnnotation(AccessPermission.class);
            if (classAnnotation == null && methodAnnotation == null){
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }
}
